﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Transactions;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using FineUI;
using Newtonsoft.Json.Linq;
using OnlineTestBLL;
using OnlineTestModel;
using Newtonsoft.Json;
namespace OnlineTestUI
{
    public partial class attendpaper : System.Web.UI.Page
    {
        protected QuestionItemsBLL qib = new QuestionItemsBLL();
        protected Papers p = new Papers();
        protected List<PaperQuestionEx> pqeList=new List<PaperQuestionEx>();
        protected Students s=new Students();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Session["student"] != null)
            {
                s = (Students)Session["student"];
                if (!IsPostBack)
                {
                    if (Request.QueryString["id"] != null)
                    {
                        int id = Convert.ToInt32(Request.QueryString["id"]);
                        BindToData(id);
                    }
                }
            }
            else
            {
                PageContext.Redirect("login.aspx");
            }
        }

        private void BindToData(int id)
        {
            PapersBLL pb = new PapersBLL();
            p = pb.GetModel(id);
            QuestionsBLL qb = new QuestionsBLL();
            pqeList = qb.GetListByPaperId(" =" + id.ToString() + " order by bigquestion,smallquestion,id");

        }
        protected void BtAdd_Click(object sender, EventArgs e)
        {
            StudentPapersBLL spb = new StudentPapersBLL();
            StudentQuestionsBLL sqb = new StudentQuestionsBLL();
            StudentPapers sp = new StudentPapers();
            QuestionsBLL qb = new QuestionsBLL();
            string paper = Request[@"paperresult"];
            JObject o = JObject.Parse(paper);
            JArray questionList = (JArray)o["question"];
            IList<StudentQuestionsEX> sqList = questionList.ToObject<IList<StudentQuestionsEX>>();
            using (TransactionScope ts = new TransactionScope())
            {
                try
                {
                    StudentQuestions sqtopaper = sqList[0];
                    sp.PaperId = Convert.ToInt32(sqtopaper.PaperId);
                    sp.StudentId = sqtopaper.StudentId;
                    sp.TestTime = DateTime.Now;
                    spb.Add(sp);
                    foreach (StudentQuestionsEX sqex in sqList)
                    {
                        if (sqex.QuestionTypeId == 1 || sqex.QuestionTypeId == 3)
                        {

                            Questions q = qb.GetModel(sqex.QuestionId);
                            q.TotalCount = q.TotalCount + 1; //答题总数加1
                            if (q.CorrectAnswer != sqex.Answer) //如果学生答案和正确答案不相符的话
                            {
                                sqex.Score = 0;
                            }
                            else
                            {
                                q.CorrectCount = q.CorrectCount + 1;
                            }
                            q.TrueDifficult = (q.CorrectCount/q.TotalCount).ToString();
                            qb.Update(q);
                        }
                        StudentQuestions sq = sqex;
                        sqb.Add(sq);
                    }
                    ts.Complete();
                    Alert.Show("提交试卷成功");
                    PageContext.Redirect("welcome.aspx", "_self");
                }
                catch (Exception)
                {
                    
                    Alert.Show("出现错误");
                }

            }


        }
    }
}